import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { List, Card, Button, Avatar, Modal, Form } from 'antd';
import moment from 'moment';
import { imageOrigin } from '../config';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import ImageUploader from '@/components/ImageUploader';
import Result from '@/components/Result';
import styles from './Pictures.less';

const FormItem = Form.Item;

@connect(({ pictures, loading }) => ({
  pictures,
  loading: loading.models.list,
}))
@Form.create()
class Pictures extends PureComponent {
  state = {
    visible: false,
    done: false,
  };

  formLayout = {
    labelCol: { span: 7 },
    wrapperCol: { span: 13 },
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'pictures/fetch',
    });
  }

  showModal = () => {
    this.setState({
      visible: true,
      current: undefined,
    });
  };

  showEditModal = item => {
    this.setState({
      visible: true,
      current: item,
    });
  };

  showEditModal = item => {
    this.setState({
      visible: true,
      current: item,
    });
  };

  handleDone = () => {
    this.setState({
      done: false,
      visible: false,
    });
  };

  handleCancel = () => {
    this.setState({
      visible: false,
    });
  };

  deleteItem = (id, currentIndex) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'pictures/remove',
      payload: { id, currentIndex },
    });
  };

  handleSearch = value => {
    const { dispatch } = this.props;
    dispatch({
      type: 'pictures/search',
      payload: value,
    });
  };

  render() {
    const {
      pictures: { data },
      loading,
    } = this.props;
    const {
      form: { getFieldDecorator },
    } = this.props;
    const { visible, done, current = {} } = this.state;

    // const editAndDelete = (currentItem, currentIndex) => {
    //   Modal.confirm({
    //     title: '删除任务',
    //     content: '确定删除该任务吗？',
    //     okText: '确认',
    //     cancelText: '取消',
    //     onOk: () => this.deleteItem(currentItem.id, currentIndex),
    //   });
    // };

    const modalFooter = done
      ? { footer: null, onCancel: this.handleDone }
      : { okText: '保存', onOk: this.handleSubmit, onCancel: this.handleCancel };

    const ListContent = ({ data: { createdAt } }) => (
      <div className={styles.listContent}>
        <div className={styles.listContentItem}>
          <span>上传时间</span>
          <p>{moment(createdAt).format('YYYY-MM-DD')}</p>
        </div>
      </div>
    );

    // const Delete = props => <a onClick={() => editAndDelete(props.current, props.index)}>删除</a>;

    const getModalContent = () => {
      if (done) {
        return (
          <Result
            type="success"
            title="操作成功"
            description="已确认添加该图片"
            actions={
              <Button type="primary" onClick={this.handleDone}>
                知道了
              </Button>
            }
            className={styles.formResult}
          />
        );
      }
      // const imageUrl = this.state.imageUrl;
      return (
        <Form onSubmit={this.handleSubmit}>
          <FormItem label="营业执照" {...this.formLayout}>
            {getFieldDecorator('fileHash', {
              rules: [{ required: false }],
              initialValue: `${current.fileHash}`,
            })(<ImageUploader />)}
            <Modal footer={null} onCancel={this.handleCancels}>
              <img alt="这是图片" style={{ width: '50%' }} />
            </Modal>
          </FormItem>
        </Form>
      );
    };
    return (
      <PageHeaderWrapper>
        <div className={styles.standardList}>
          <Card
            className={styles.listCard}
            bordered={false}
            title="图片管理"
            style={{ marginTop: 24 }}
            bodyStyle={{ padding: '0 32px 40px 32px' }}
          >
            <List
              size="large"
              rowKey="id"
              loading={loading}
              pagination={{
                ...data.pagination,
                onChange: page => {
                  const { dispatch } = this.props;
                  dispatch({
                    type: 'pictures/fetch',
                    payload: page,
                  });
                },
              }}
              dataSource={data.list}
              item={data.list}
              renderItem={item => (
                <List.Item>
                  <List.Item.Meta
                    avatar={
                      <Avatar src={`${imageOrigin}${item.hash}`} shape="square" size="large" />
                    }
                    description={item.intro}
                  />
                  <ListContent data={item} />
                </List.Item>
              )}
            />
          </Card>
        </div>
        <Modal
          title={done ? null : `店铺${current ? '添加' : '编辑'}`}
          className={styles.standardListForm}
          width={640}
          bodyStyle={done ? { padding: '72px 0' } : { padding: '28px 0 0' }}
          destroyOnClose
          visible={visible}
          {...modalFooter}
        >
          {getModalContent()}
        </Modal>
      </PageHeaderWrapper>
    );
  }
}

export default Pictures;
